#include <sw/redis++/redis++.h>
#include <iostream>
#include <vector>
#include <set>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    cout << "sadd and smembers" << endl;
    redis.flushall();

    redis.sadd("key", "111");
    redis.sadd("key", {"111", "222", "333", "444"});

    std::set<string> values = {"555", "666", "777"};
    redis.sadd("key", values.begin(), values.end());

    vector<string> result;
    // auto it = std::back_inserter(result);
    auto it = std::inserter(result, result.end()); // 使用insert_iterator可以支持set的使用
    redis.smembers("key", it);

    for (int i = 0; i < result.size(); i++)
        cout << result[i] << endl;
}

void test2(Redis &redis)
{
    cout << "sismember" << endl;
    redis.flushall();

    redis.sadd("key", {"111", "222", "333"});

    auto result = redis.sismember("key", "666");
    cout << result << endl;
}

void test3(Redis &redis)
{
    cout << "scard" << endl;
    redis.flushall();

    redis.sadd("key", {"111", "222", "333"});

    auto ret = redis.scard("key");
    cout << ret << endl;
}

void test4(Redis& redis)
{
    cout << "spop" << endl;
    redis.flushall();

    redis.sadd("key",{"111","222","333","444"});

    auto ret = redis.spop("key");
    if(ret)
        cout << ret.value() << endl;
}

void test5(Redis& redis)
{
    std::cout << "sinter" << endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333","444"});
    redis.sadd("key2",{"111","222","555"});

    std::set<string> result;
    auto it = std::inserter(result,result.end());
    redis.sinter({"key1","key2"},it);

    for(auto e : result)
        cout << e << endl;
}

void test6(Redis& redis)
{
    cout << "sinterstore" << endl;
    redis.flushall();

    redis.sadd("key1",{"111","222","333"});
    redis.sadd("key2",{"111","333","444"});

    long long len = redis.sinterstore("key3",{"key1","key2"});
    cout << "len: " << len << endl;

    std::set<string> result;
    auto it = std::inserter(result,result.end());
    redis.smembers("key3",it);

    for(auto e : result)
        cout << e << endl;

}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    //test4(redis);
    //test5(redis);
    test6(redis);
    return 0; 
}